package com.sinog.auth.repository;

import com.sinog.auth.entity.AuthUser;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import javax.transaction.Transactional;

/**
 * @desc 获取用户信息的接口 auth_user
 * @author zy
 * @date 2019年8月15日 11点59分
 */
public interface UserRepository extends BaseRepository<AuthUser> {

    /**
     * 通过用户账号和状态查询用户
     * @param account account
     * @param stat stat
     * @return AuthUser
     */
    AuthUser findUserByAccountAndStatIs(String account,Integer stat);

    /**
     * 通过账号查询密码
     * @param account account
     * @return String
     */
    @Query("select u.password from AuthUser u where u.account=:account and u.stat=1 ")
    String findPasswordByAccount(@Param("account") String account);

    /**
     * 更新密码
     * @param account account
     * @param pwd pwd
     * @return int
     */
    @Modifying
    @Transactional(rollbackOn = Exception.class)
    @Query("update AuthUser u set u.password=:pwd where u.account=:account ")
    int updatePasswordByAccount(@Param("account") String account,@Param("pwd") String pwd);

    /**
     * 启用/禁用 用户
     * @param account account
     * @param stat stat
     * @return int
     */
    @Modifying
    @Transactional(rollbackOn = Exception.class)
    @Query("update AuthUser u set u.stat=:stat where u.account=:account ")
    int updateStatByAccount(@Param("account") String account,@Param("stat") Integer stat);

    /**
     * 添加授权用户
     * @param account account
     * @param password password
     */
    @Modifying
    @Transactional(rollbackOn = Exception.class)
    @Query(value = "REPLACE INTO auth_user(account,password) values(?1,?2)", nativeQuery = true)
    void insertAuthUser(@Param("account") String account,@Param("password") String password);
}